package com.youdianstar.app.common.widget;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * File: SheetCheckHelper
 * Author: 82149 Create: 2021/10/11 17:39
 * Changes (from 2021/10/11)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class SheetCheckHelper extends CheckHelper{
    private RecyclerView recyclerView;
    private int checked = -1;
    private boolean isHide = false;

    public SheetCheckHelper(HandleStateChange handleStateChange) {
        super(handleStateChange);
    }

    public SheetCheckHelper(HandleStateChange handleStateChange, RecyclerView recyclerView) {
        super(handleStateChange);
        this.recyclerView = recyclerView;
    }

    public SheetCheckHelper(HandleStateChange handleStateChange, RecyclerView recyclerView, int checked) {
        super(handleStateChange);
        this.recyclerView = recyclerView;
        this.checked = checked;
    }

    public SheetCheckHelper(HandleStateChange handleStateChange, RecyclerView recyclerView, int checked,boolean isHide) {
        super(handleStateChange);
        this.recyclerView = recyclerView;
        this.checked = checked;
        this.isHide = isHide;
    }


    @Override
    void bindViewHolder(RecyclerView.ViewHolder viewHolder, View clickView) {
        final int position = viewHolder.getAdapterPosition();
        if (isHide && position < checked){
            clickView.setAlpha(0.5f);
            clickView.setEnabled(false);
        }
        clickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean contain = isCheckedPosition(position);
                if (contain) {
                    handleChange(viewHolder, true);
                } else {
                    if (checked != -1) {
                        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(checked);
                        if (holder != null) {
                            stateChange(holder, false);
                        }
                    }
                    checked = position;
                    stateChange(viewHolder, true);
                }
                clickWhich(position);
            }
        });
        stateChange(viewHolder, isCheckedPosition(position));
    }

    @Override
    boolean isCheckedPosition(int position) {
        return position == checked;
    }

    public void setChecked(int checked) {
        this.checked = checked;
    }
}
